package com.zzyl.nursing.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.utils.PDFUtil;
import com.zzyl.common.utils.uuid.UUID;
import com.zzyl.nursing.dto.HealthAssessmentDto;
import com.zzyl.oss.client.OSSAliyunFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.service.IHealthAssessmentService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 健康评估Controller
 *
 * @author Nan
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/nursing/healthAssessment")
@Api(tags = "健康评估的接口")
public class HealthAssessmentController extends BaseController
{
    @Autowired
    private IHealthAssessmentService healthAssessmentService;


    // 注入oss
    @Autowired
    private OSSAliyunFileStorageService ossAliyunFileStorageService;

    // 注入redis 对象
    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 上传体检报告的接口
     */
    @ApiOperation("上传体检报告")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    @Log(title = "健康评估", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult add(@ApiParam(value = "健康评估实体")
                          String idCardNo, MultipartFile file)
    {
        try {
            String originalFilename = file.getOriginalFilename();

            String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

            String url = ossAliyunFileStorageService.store(fileName, file.getInputStream());

            // 2， 把文件名和 url 放到返回给前端的数据中
            AjaxResult successAjax = AjaxResult.success(); //继承了 hashMap 所以我们可以手动添加属性
            successAjax.put("url",url);
            successAjax.put("fileName",url);
            successAjax.put("originalFilename",originalFilename);

            // 利用工具类 解析pdf 中的内容 PDFUtil.pdfToString(输入流对象)
            String content = PDFUtil.pdfToString(file.getInputStream());

            //  把解析出来的内容 存在redis 中

            redisTemplate.boundHashOps("healthReport").put(idCardNo,content);

            // 把结果return 出去
            return successAjax;
        } catch (IOException e) {
            e.printStackTrace();
            // 有错误抛出
            return  AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询健康评估列表
     */
    /**
     * 查询健康评估列表
     */
    @ApiOperation("查询健康评估列表")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:list')")
    @GetMapping("/list")
    public TableDataInfo list(HealthAssessment healthAssessment) {
        startPage();
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        return getDataTable(list);
    }
    /**
     * 导出健康评估列表
     */
    @ApiOperation("导出健康评估列表")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:export')")
    @Log(title = "健康评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthAssessment healthAssessment)
    {
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        ExcelUtil<HealthAssessment> util = new ExcelUtil<HealthAssessment>(HealthAssessment.class);
        util.exportExcel(response, list, "健康评估数据");
    }

    /**
     * 获取健康评估详细信息
     */
    @ApiOperation("获取健康评估详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "健康评估ID", required = true)
            @PathVariable("id") Long id)
    {
        return success(healthAssessmentService.selectHealthAssessmentById(id));
    }

    /**
     * 新增健康评估
     */
    /**
     * 新增健康评估
     */

    @ApiOperation("新增健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    @Log(title = "健康评估", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "健康评估实体")
                          @RequestBody HealthAssessmentDto dto) {
        // 因为前端需要1个id.所以我们要返回1个id,这个id 就是新增成功后得到的id
      Long id =  healthAssessmentService.insertHealthAssessmentNew(dto);
      return success(id);
        // return toAjax(healthAssessmentService.insertHealthAssessment(healthAssessment));
    }
    // @ApiOperation("新增健康评估")
    // @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    // @Log(title = "健康评估", businessType = BusinessType.INSERT)
    // @PostMapping
    // public AjaxResult add(@ApiParam(value = "健康评估实体")
    //                       @RequestBody HealthAssessment healthAssessment) {
    //     return toAjax(healthAssessmentService.insertHealthAssessment(healthAssessment));
    // }

    /**
     * 修改健康评估
     */
    @ApiOperation("修改健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:edit')")
    @Log(title = "健康评估", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "健康评估实体")
            @RequestBody HealthAssessment healthAssessment)
    {
        return toAjax(healthAssessmentService.updateHealthAssessment(healthAssessment));
    }

    /**
     * 删除健康评估
     */
    @ApiOperation("删除健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:remove')")
    @Log(title = "健康评估", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(healthAssessmentService.deleteHealthAssessmentByIds(ids));
    }
}
